package com.oracle.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.oracle.entity.po.DepartmentPO;
import com.oracle.service.department.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @RequestMapping(value = "getPage", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPage(@RequestParam("limit") int limit, @RequestParam("curr") int curr, HttpServletRequest request, HttpServletResponse response) {
        Page<DepartmentPO> page = new Page<>(curr, limit);
        List list =departmentService.selectPage(page, null).getRecords();
        Integer count = departmentService.selectCount(null);
        Map<String, Object> jsonmap = new HashMap<String, Object>();
        jsonmap.put("code", 0);
        jsonmap.put("data", list);
        jsonmap.put("count", count);
        jsonmap.put("msg", "查询成功");
        return jsonmap;
    }

    @RequestMapping(value = "/editDepartment",method = RequestMethod.POST)
    @ResponseBody
    public Map<String ,Object> editDepartment(@RequestBody String data, HttpServletRequest request, HttpServletResponse response){
        JSONObject jsonObject = JSON.parseObject(data);
        DepartmentPO departmentPO = new DepartmentPO();
        departmentPO.setId(jsonObject.getInteger("id"));
        departmentPO.setName(jsonObject.getString("name"));
        departmentService.updateById(departmentPO);
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("msg","修改成功");
        return jsonMap;
    }

    @RequestMapping(value = "/deleteDepartment",method = RequestMethod.POST)
    @ResponseBody
    public Map<String ,Object> deleteDepartment(@RequestBody String data, HttpServletRequest request, HttpServletResponse response){
        JSONArray jsonArray = JSON.parseArray(data);
        JSONObject jsonObject=jsonArray.getJSONObject(0);
        int id = jsonObject.getInteger("id");
        departmentService.deleteById(id);
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("code",0);
        jsonMap.put("msg","删除成功");
        return jsonMap;
    }

    @RequestMapping(value = "/addDepartment",method = RequestMethod.POST)
    @ResponseBody
    public Map<String ,Object> addDepartment(@RequestBody String data, HttpServletRequest request, HttpServletResponse response){
        JSONObject jsonObject = JSON.parseObject(data);
        DepartmentPO departmentPO = new DepartmentPO();
        departmentPO.setId(jsonObject.getInteger("id"));
        departmentPO.setName(jsonObject.getString("name"));
        departmentService.insert(departmentPO);
        Map<String,Object> jsonMap = new HashMap<>();
        jsonMap.put("msg","添加成功");
        return jsonMap;
    }



}